using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;

namespace PrismDemo.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private readonly IRegionManager _regionManager;
        private readonly IDialogService _dialogService;
        private string _statusMessage = "准备就绪";

        public MainWindowViewModel(IRegionManager regionManager, IDialogService dialogService)
        {
            _regionManager = regionManager;
            _dialogService = dialogService;

            // 初始化Command
            NavigateToUsersCommand = new DelegateCommand(NavigateToUsers);
            NavigateToAddUserCommand = new DelegateCommand(NavigateToAddUser);
            NavigateToProductsCommand = new DelegateCommand(NavigateToProducts);
            NavigateToAddProductCommand = new DelegateCommand(NavigateToAddProduct);
            ShowConfirmDialogCommand = new DelegateCommand(ShowConfirmDialog);
            ShowNotificationDialogCommand = new DelegateCommand(ShowNotificationDialog);
            ShowAboutDialogCommand = new DelegateCommand(ShowAboutDialog);

            // 默认导航到欢迎页面
            NavigateToWelcome();
        }

        #region Properties

        public string StatusMessage
        {
            get { return _statusMessage; }
            set { SetProperty(ref _statusMessage, value); }
        }

        #endregion

        #region Commands

        public DelegateCommand NavigateToUsersCommand { get; }
        public DelegateCommand NavigateToAddUserCommand { get; }
        public DelegateCommand NavigateToProductsCommand { get; }
        public DelegateCommand NavigateToAddProductCommand { get; }
        public DelegateCommand ShowConfirmDialogCommand { get; }
        public DelegateCommand ShowNotificationDialogCommand { get; }
        public DelegateCommand ShowAboutDialogCommand { get; }

        #endregion

        #region Command Methods

        private void NavigateToUsers()
        {
            _regionManager.RequestNavigate("ContentRegion", "UserListView");
            StatusMessage = "导航到用户列表";
        }

        private void NavigateToAddUser()
        {
            _regionManager.RequestNavigate("ContentRegion", "AddUserView");
            StatusMessage = "导航到添加用户";
        }

        private void NavigateToProducts()
        {
            _regionManager.RequestNavigate("ContentRegion", "ProductListView");
            StatusMessage = "导航到产品列表";
        }

        private void NavigateToAddProduct()
        {
            _regionManager.RequestNavigate("ContentRegion", "AddProductView");
            StatusMessage = "导航到添加产品";
        }

        private void ShowConfirmDialog()
        {
            var parameters = new DialogParameters
            {
                { "title", "确认" },
                { "message", "您确定要执行此操作吗？" }
            };

            _dialogService.ShowDialog("ConfirmationDialog", parameters, result =>
            {
                if (result.Result == ButtonResult.OK)
                {
                    StatusMessage = "用户点击了确认";
                }
                else
                {
                    StatusMessage = "用户取消了操作";
                }
            });
        }

        private void ShowNotificationDialog()
        {
            var parameters = new DialogParameters
            {
                { "title", "通知" },
                { "message", "这是一个通知消息！" }
            };

            _dialogService.ShowDialog("NotificationDialog", parameters, result =>
            {
                StatusMessage = "通知对话框已关闭";
            });
        }

        private void ShowAboutDialog()
        {
            var parameters = new DialogParameters
            {
                { "title", "关于" },
                { "message", "PRISM 学习演示程序\n版本: 1.0.0\n作者: 学习者" }
            };

            _dialogService.ShowDialog("NotificationDialog", parameters, result =>
            {
                StatusMessage = "关于对话框已关闭";
            });
        }

        #endregion

        #region Private Methods

        private void NavigateToWelcome()
        {   
            // 默认导航到欢迎页面
            // 导航的本质仍然是通过View与ViewModel的映射关系，找到ViewModel
            // 并把实例赋予ContentControl的Content属性
            _regionManager.RequestNavigate("ContentRegion", "WelcomeView");
            StatusMessage = "欢迎使用 PRISM 学习演示系统";
        }

        #endregion
    }
}